package test5.lupeng.jf.com.teamdemo.activitys;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.TitleCheckAdapter;
import test5.lupeng.jf.com.teamdemo.bean.RequestTitleCheck;
import test5.lupeng.jf.com.teamdemo.bean.response.ResponseTitleCheck;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

//文章评论列表
public class TitleCheckActivity extends AppCompatActivity {

    private static final String TAG = "TitleCheckActivity";
    private RecyclerView titleCheckRv;
    private String articleId;
    private int articleType;
    ArrayList<ResponseTitleCheck.ResultBean> results;
    private TitleCheckAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title_check);

        titleCheckRv = (RecyclerView) findViewById(R.id.titleCheckRv);

        articleId = getIntent().getStringExtra("articleId");
        articleType = getIntent().getIntExtra("articleType", 0);

        results = new ArrayList<>();

        //获取评论
        RequestTitleCheck();

        adapter = new TitleCheckAdapter(results);
        titleCheckRv.setLayoutManager(new LinearLayoutManager(this));
        titleCheckRv.setAdapter(adapter);

    }

    public void RequestTitleCheck(){
        String url = "https://leancloud.cn:443/1.1/functions/getJFInfoComments";
        RequestTitleCheck check = new RequestTitleCheck(articleId, 10, 0, articleType + "");
        Request request = HTTPUtils.requestPost(check, url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: "+result );
                ResponseTitleCheck bean = new Gson().fromJson(result, ResponseTitleCheck.class);
                results.addAll(bean.getResult());

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });

            }
        });
    }





    //返回按钮
    public void newReturn3(View view) {
        finish();
    }
}
